package com.yoyosys.db.partition;

import com.yoyosys.db.partition.constant.DBType;
import com.yoyosys.db.partition.query.PartitionQuery;
import com.yoyosys.db.partition.query.impl.DaMengPartitionQuery;
import com.yoyosys.db.partition.query.impl.HanaPartitionQuery;
import com.yoyosys.db.partition.query.impl.MySqlPartitionQuery;
import com.yoyosys.db.partition.query.impl.PostgreSqlPartitionQuery;
import com.yoyosys.db.partition.vo.PartitionInfo;
import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Arrays;
import java.util.List;

/**
 * 分区分桶信息查询工具
 */
@Slf4j
public class DBPartitionUtil {

    /**
     * 查询表分区信息
     * @param dbType
     * @param conn 数据库连接，注意查询结束connection不会关闭，需要在外部关闭
     * @param schemaName
     * @param tableName
     * @return
     * @throws Exception
     */
    public static PartitionInfo getPartition(DBType dbType, Connection conn, String schemaName, String tableName) throws Exception {
        log.info("getPartition - dbType:{}, schemaName:{}, tableName:{}", dbType, schemaName, tableName);
        return getQuery(dbType).getPartition(conn, schemaName, tableName);
    }

    /**
     * 查询表分区信息
     * @param dbType
     * @param jdbcUrl
     * @param username
     * @param password
     * @param schemaName
     * @param tableName
     * @return
     * @throws Exception
     */
    public static PartitionInfo getPartition(DBType dbType, String jdbcUrl, String username, String password,
                                             String schemaName, String tableName) throws Exception {
        log.info("getPartition - dbType:{}, jdbcUrl:{}, username:{}, password:{}, schemaName:{}, tableName:{}",
                dbType, jdbcUrl, username, password, schemaName, tableName);

        try (Connection conn = DriverManager.getConnection(jdbcUrl, username, password)) {
            return getQuery(dbType).getPartition(conn, schemaName, tableName);
        }
    }


    /**
     * 根据数据库类型获取对应的查询器
     * @param dbType
     * @return
     */
    private static PartitionQuery getQuery(DBType dbType){
        switch (dbType) {
            case MYSQL:
                return MySqlPartitionQuery.getInstance();
            case DM:
                return DaMengPartitionQuery.getInstance();
            case POSTGRESQL:
                return PostgreSqlPartitionQuery.getInstance();
            case HANA:
                return HanaPartitionQuery.getInstance();
            default:
                throw new UnsupportedOperationException("不支持的数据库类型: " + dbType);
        }
    }

    public static void main(String[] args) throws Exception {
        testMysql();
        //testPg();
    }

    private static void testMysql()  throws Exception{
        String jdbcUrl = "jdbc:mysql://192.168.100.102:3306/test";
        String username = "root";
        String password = "root";
        String schemaName = "test2";
        try (Connection conn = DriverManager.getConnection(jdbcUrl, username, password)) {
            List<String> tables = Arrays.asList("users", "sales", "log_entries", "employees");
            for (String tableName : tables) {
                PartitionInfo partitionInfo = getQuery(DBType.MYSQL).getPartition(conn, schemaName, tableName);
                System.out.println("\n" + tableName + " 分区信息: " + partitionInfo.toJson());
            }
        }
    }


    private static void testPg() throws Exception{
        String jdbcUrl = "jdbc:postgresql://192.168.100.1:5432/";
        String username = "postgres";
        String password = "postgres";
        String schemaName = "test2";
        try (Connection conn = DriverManager.getConnection(jdbcUrl, username, password)) {
            List<String> tables = Arrays.asList("users", "sales", "employees");
            for (String tableName : tables) {
                PartitionInfo partitionInfo = getQuery(DBType.POSTGRESQL).getPartition(conn, schemaName, tableName);
                System.out.println("\n" + tableName + " 分区信息: " + partitionInfo.toJson());
            }
        }
    }
}
